package com.atguigu.tingshu.account.receiver;

import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @author atguigu-mqx
 * @ClassName AccountReceiver
 * @description: TODO
 * @date 2025年04月13日
 * @version: 1.0
 */
@Component
@Slf4j
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService;

    @Autowired
    private RechargeInfoService rechargeInfoService;

    /**
     * 监听充值成功消息
     * @param orderNo
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_RECHARGE_PAY_SUCCESS, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ORDER),
            key = MqConst.ROUTING_RECHARGE_PAY_SUCCESS
    ))
    public void rechargePaySuccess(String orderNo, Message message, Channel channel) {
        try {
            //  判断订单编号.
            if (!StringUtils.isEmpty(orderNo)){
                //  调用服务层方法;
                rechargeInfoService.rechargePaySuccess(orderNo);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        //  手动确认：
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 监听消息; 1.监听消息作用; 2.初始化队列，交换机，路由键三者之间的绑定关系;
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_USER_REGISTER, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_USER),
            key = MqConst.ROUTING_USER_REGISTER
    ))
    public void register(Long userId, Message message, Channel channel) {
        System.out.println("用户注册成功，发送消息给用户服务");
        //  如果消费者处理业务逻辑时，出现了异常.那么我们要不要手动签收消息?
        //  判断消息是否正确接收到;
        try {
            if (null != userId) {
                //  调用服务层方法，进行初始化账户金额;
                userAccountService.initAccount(userId);
            }
        } catch (Exception e) {
            //  落盘到数据异常信息表; exmsg();
            log.error(e.getMessage());
            //  利用redis 记录重试次数； 达到3次，则记录异常消息表;
            //  try {
            //      channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            //  } catch (IOException ex) {
            //      throw new RuntimeException(ex);
            //  }
            //  return;
        }
        //  手动确认：
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
